package com.wxh.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @ClassName: DateUtils
 * @Description: 日期工具类型
 * @Author wxh
 * @Date: 2019/11/11 17:31
 * @Version V1.0.0
 * @Since 1.8
 */
public class DateUtils2 {

    private DateUtils2(){

    }

    /**
     * 求某个时间前N天的时间字符串
     * @param dateString 传入时间入参
     * @param days 前几天
     * @param pattern 格式,比如 "yyyyMMddHH"
     * @return String
     */
    public static String getDateBefore(String dateString, int days, String pattern){
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(pattern);
            Date date = sdf.parse(dateString);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            calendar.add(Calendar.DAY_OF_MONTH, -days);
            Date dBefore = calendar.getTime();
            String dateStr = sdf.format(dBefore);
            return dateStr;
        } catch (ParseException e) {
            throw new IllegalArgumentException("parameter date is not valid");
        }
    }

    /**
     * 根据传入的时间求上周日的时间
     * @param dataStr 传入时间入参
     * @return String
     */
    public static String getPrevSundayDate(String dataStr){
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHH");
            Date time = sdf.parse(dataStr);
            Calendar calendar2 = Calendar.getInstance();
            calendar2.setTime(time);
            int dayOfWeek = calendar2.get(Calendar.DAY_OF_WEEK) - 1;
            int offset2 = 7 - dayOfWeek;
            calendar2.add(Calendar.DATE, offset2 - 7);
            return sdf.format(calendar2.getTime());
        } catch (ParseException e) {
            throw new IllegalArgumentException("parameter date is not valid");
        }
    }

    /**
     * 根据传入的时间求上周一的时间
     * @param dataStr 传入时间入参
     * @return String
     */
    public static String getPrevMondayDate(String dataStr){
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHH");
            Calendar calendar1 = Calendar.getInstance();
            Date time = sdf.parse(dataStr);
            calendar1.setTime(time);
            int dayOfWeek = calendar1.get(Calendar.DAY_OF_WEEK) - 1;
            int offset1 = 1 - dayOfWeek;
            calendar1.add(Calendar.DATE, offset1 - 7);
            return sdf.format(calendar1.getTime());
        } catch (ParseException e) {
            throw new IllegalArgumentException("parameter date is not valid");
        }
    }


    /**
     * 根据周一的时间取周日的时间
     * @param dataStr 传入的时间字符串
     * @return String
     */
    public static String getSundayDate(String dataStr){
        try {
            // 设置时间格式
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHH");
            Calendar cal = Calendar.getInstance();
            Date time =  sdf.parse(dataStr);
            cal.setTime(time);
            cal.add(Calendar.DATE, 6);
            return sdf.format(cal.getTime());
        } catch (ParseException e) {
            throw new IllegalArgumentException("parameter date is not valid");
        }
    }

    /**
     * 根据传入的时间获取周一的时间字符串
     * @param dataStr 传入的时间字符串
     * @return String
     */
    public static String getMondayDate(String dataStr){
        try {
            // 设置时间格式
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHH");
            Calendar cal = Calendar.getInstance();
            Date time =  sdf.parse(dataStr);
            cal.setTime(time);
            // 判断要计算的日期是否是周日，如果是则减一天计算周六的，否则会出问题，计算到下一周去了
            int dayWeek = cal.get(Calendar.DAY_OF_WEEK);
            // 获得当前日期是一个星期的第几天
            if(1 == dayWeek) {
                cal.add(Calendar.DAY_OF_MONTH, -1);
            }
            // 设置一个星期的第一天，按中国的习惯一个星期的第一天是星期一
            cal.setFirstDayOfWeek(Calendar.MONDAY);
            // 获得当前日期是一个星期的第几天
            int day = cal.get(Calendar.DAY_OF_WEEK);
            // 根据日历的规则，给当前日期减去星期几与一个星期第一天的差值
            cal.add(Calendar.DATE, cal.getFirstDayOfWeek()-day);
            return sdf.format(cal.getTime());
        } catch (ParseException e) {
            throw new IllegalArgumentException("parameter date is not valid");
        }
    }

    /**
     * 求某个时间前N天的时间字符串
     * @param date 传入时间入参
     * @param days 前几天
     * @return String
     */
    public static String getDateBefore(Date date, int days){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_MONTH, -days);
        Date dBefore = calendar.getTime();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateStr = sdf.format(dBefore);
        return dateStr;
    }

    /**
     * 将时间字符串转换为时间格式，格式如：yyyy-MM-dd HH:mm:ss
     * @param dateStr 时间字符串
     * @return Date  时间
     */
    public static Date stringToDate(String dateStr){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            return sdf.parse(dateStr);
        } catch (ParseException e) {
            throw new IllegalArgumentException("parameter date is not valid");
        }
    }

    /**
     * 获取某个时间，上一年的对应时间
     * @param date 时间
     * @return String 字符串
     */
    public static String currentWithLastYearDate(Date date){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.YEAR, -1);
        Date lastDate = calendar.getTime();
        String lastYearDateStr = sdf.format(lastDate);
        return lastYearDateStr;
    }

    /**
     * 求某个时间前N个的时间字符串
     * @param date 传入时间入参
     * @param hours 前几小时
     * @return String
     */
    public static String getHourBefore(Date date, int hours){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY) - hours);
        Date dBefore = calendar.getTime();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateStr = sdf.format(dBefore);
        return dateStr;
    }

    /**
     * 获取系统当前时间
     * @return String
     */
    public static String getSystemCurrentDate(){
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date dBefore = calendar.getTime();
        String dateStr = sdf.format(dBefore);
        return dateStr;
    }

    /**
     * 两个日期相差的天数
     * @param date1 日期1
     * @param date2 日期2
     * @return int
     */
    public static int differentDays(Date date1,Date date2){
        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(date1);

        Calendar cal2 = Calendar.getInstance();
        cal2.setTime(date2);
        int day1= cal1.get(Calendar.DAY_OF_YEAR);
        int day2 = cal2.get(Calendar.DAY_OF_YEAR);

        int year1 = cal1.get(Calendar.YEAR);
        int year2 = cal2.get(Calendar.YEAR);
        //同一年
        if(year1 != year2) {
            int timeDistance = 0 ;
            for(int i = year1 ; i < year2 ; i ++) {
                //闰年
                if(i%4 == 0 && i%100 !=0 || i%400 == 0) {
                    timeDistance += 366;
                } else {
                    //不是闰年
                    timeDistance += 365;
                }
            }
            return timeDistance + (day2-day1) ;
        } else {
            //不同年
            return day2-day1;
        }
    }

    /**
     * 字符串转换日期
     * @param dateStr 字符串
     * @return Date
     */
    public static Date getStringToDate(String dateStr){
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date date = sdf.parse(dateStr);
            return date;
        } catch (ParseException e) {
            throw new java.lang.IllegalArgumentException("parameter date is not valid");
        }
    }

    /**
     * 获取系统当前时间
     * @return String
     */
    public static String getSystemCurrentTime(){
        Calendar calendar = Calendar.getInstance();
        Date date = calendar.getTime();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String dateStr = sdf.format(date);
        return dateStr;
    }

    public static void main(String[] args) {
        String time = "2019-10-10 01:00:00";
        Date date = stringToDate(time);

        String dateStr =  getDateBefore(date,7);
        System.out.println(dateStr);

        String lastYearStr = currentWithLastYearDate(date);
        System.out.println(lastYearStr);

        String sixTime = getHourBefore(date,3);
        System.out.println(sixTime);

        System.out.println(getSystemCurrentDate());

        Date currTime = getStringToDate(getSystemCurrentTime());
        Date time2 = getStringToDate("2022-04-01");
        System.out.println(differentDays(time2, currTime));
    }
}
